<?php
require_once PATH_SITE.'cn/royan/php/utils/Logger.util.php';
require_once PATH_SITE.'cn/royan/php/utils/Search.util.php';
require_once PATH_SITE.'cn/royan/php/utils/Plugins.util.php';
require_once PATH_SITE.'cn/royan/php/parses/ValidLoginParse.parse.php';
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserEnterRoomParse
 *
 * @author yanr
 */
class UserEnterRoomParse extends ValidLoginParse
{
    //put your code here
    const TYPE          = "UserEnterRoom";
    const CHANGE        = "RoomUserChange";
    const USER_ENTER    = "UserEnter";
    const ERROR_10001   = "error_10001";
    const ERROR_10002   = "error_10002";
    const ERROR_10003   = "error_10003";
    
    protected $list;
    
//    static public function getInstance()
//    {
//        IF( is_null( self::$instance ) ) self::$instance = new UserEnterRoomParse( UserEnterRoomParse::TYPE );
//        RETURN self::$instance;
//    }
    
    public function __construct($typeName = NULL) {
        parent::__construct( UserEnterRoomParse::TYPE );
        Logger::trace( "UserEnterRoomParse Open" );
    }
    
    public function parseInfo($info) {
        IF( parent::parseInfo($info) === FALSE ) RETURN FALSE;
        
        Logger::trace( "User Enter Room" );
        $pluginClass = Plugins::findPlugin( UserEnterRoomParse::TYPE );

        IF ( $pluginClass !== FALSE )
        {
            $plugin = new $pluginClass( $info );
            $feedback = $plugin->excute();
        }
        $users = $info["room"]->__get("users");
        array_push( $users, $info["user"] );
        $info["room"]->__set( "users", $users );
    }
    
    protected function isValid($info) {
        IF ( parent::isValid($info) === FALSE ) RETURN FALSE;
        
        $socketProxy = $this->facade->retrieveProxy( SocketProxy::NAME );
        $socket = $socketProxy->getData();
            
        IF( Search::ValidAreaInSocket( $socket, $info["area"] ) )
        {
            IF( Search::ValidRoomInArea( $info["area"], $info["room"] ) )
            {
                IF( Search::ValidUserInRoom( $info["room"], $info["user"] ) )
                {
                    $data = array( "event"=>    BuilderEvent::ERROR,
                                   "code"=>     UserEnterRoomParse::ERROR_10002,
                                   "target"=>   UserEnterRoomParse::TYPE,
                                   "user"=>     $info["user"]);

                    $this->sendNotification( ApplicationFacade::DISPATCH_EVENT, $data );
                    RETURN FALSE;
                }
                ELSE{
                    IF( is_null( $info["room"]->__get("password") ) === FALSE &&
                        $info["room"]->__get("password") !== $info["password"] )
                    {
                        $data = array( "event"=>    BuilderEvent::ERROR,
                                       "code"=>     UserEnterRoomParse::ERROR_10003,
                                       "target"=>   UserEnterRoomParse::TYPE,
                                       "user"=>     $info["user"]);

                        $this->sendNotification( ApplicationFacade::DISPATCH_EVENT, $data );
                        RETURN FALSE;
                    }
                }
            }
            ELSE{
                $data = array( "event"=>    BuilderEvent::ERROR,
                               "code"=>     UserEnterRoomParse::ERROR_10002,
                               "target"=>   UserEnterRoomParse::TYPE,
                               "user"=>     $info["user"]);

                $this->sendNotification( ApplicationFacade::DISPATCH_EVENT, $data );
                RETURN FALSE;
            }
        }
        ELSE{
            $data = array( "event"=>    BuilderEvent::ERROR,
                           "code"=>     UserEnterRoomParse::ERROR_10001,
                           "target"=>   UserEnterRoomParse::TYPE,
                           "user"=>     $info["user"]);
            $this->sendNotification( ApplicationFacade::DISPATCH_EVENT, $data );
            RETURN FALSE;
        }
        
        RETURN TRUE;
    }
}

?>
